package com.vastcom.provider.action;

import com.vastcom.provider.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.Queue;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.UUID;

@RestController
public class PublicAction {

    @Autowired
    private Queue queue;

    @Autowired
    private Queue objectqueue;

    @Autowired
    private JmsTemplate jmsTemplate;


    @GetMapping("getObjectTest")
    @ResponseBody
    public HashMap getObjectTest() throws Exception{

        HashMap m=new HashMap();
        UserInfo u=new UserInfo();
        u.setId("654654654654");
        u.setLastlogin(new Timestamp(System.currentTimeMillis()));
        u.setPassword("*********");
        u.setUsername("albatross");
        m.put("msg","ok");
        m.put("obj",u);


        return m;
    }

    @PostMapping("postObjectTest")
    @ResponseBody
    public HashMap postObjectTest(@RequestBody UserInfo userInfo)throws Exception{
        HashMap m=new HashMap();
        UserInfo u=new UserInfo();
        u.setId(userInfo.getId());
        u.setLastlogin(new Timestamp(System.currentTimeMillis()));
        u.setPassword("*********");
        u.setUsername(userInfo.getUsername());
        m.put("msg","ok");
        m.put("obj",u);

        return m;
    }


    @GetMapping("sendMessageTest")
    @ResponseBody
    public HashMap sendMessageTest() throws Exception{
        HashMap m=new HashMap();

        try {
            jmsTemplate.convertAndSend(queue,"TextMessageText");
            m.put("msg","ok");
        } catch (JmsException e) {
            e.printStackTrace();
            m.put("msg","error");
        }

        return m;
    }

    @GetMapping("sendObjectMessage")
    @ResponseBody
    public HashMap sendObjectMessage() throws Exception{
        HashMap m=new HashMap();
        UserInfo u=new UserInfo();
        u.setUsername("userName123");
        u.setPassword("*********");
        u.setLastlogin(new Timestamp(System.currentTimeMillis()));
        u.setId(UUID.randomUUID().toString());
        jmsTemplate.convertAndSend(objectqueue,u);



        return m;
    }

}
